<?php

namespace App\Http\Controllers\Backend;


use App\Services\Business\Models\profession;
use App\Http\Requests\Company\ProfessionRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProfessionController extends BaseController
{

    public function index(Request $request)
    {


        $companyId =  $this->loginCompanyId();
        $cid = (int)$request->input('cid');
        $videos = Profession::query()
            ->when($cid, function ($query) use ($cid) {
                $query->where('category_id', '=', $cid);
            })
            ->where('company_id',$companyId)
            ->orderByDesc('created_at')
            ->paginate($request->input('size', 10));

        return $this->successData($videos);
    }



    public function store(ProfessionRequest $request, Profession $profession)
    {

        $data = $request->filldata();
        $profession->fill($data)->save();
        return $this->success(__('新增成功'));
    }

    public function update(ProfessionRequest $request, $id)
    {

        $paper = profession::query()->where('id', $id)->firstOrFail();
        $paper->fill($request->filldata())->update();
        return $this->success(__('更新成功'));
    }

    public function delete($id)
    {


        if (DB::table('group_profession')->where('chat_id', $id)->exists()) {
            return $this->error(__('职业已关联班级，无法删除'));
        }
        Profession::destroy($id);
        return $this->success(__('删除成功'));
    }

}